/*
为命令行参数调用 gethostbyname 。
**/

#include <error.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stddef.h>


int
main(int argc, char **argv) {
    if (argc == 1)
        error_quit("usage: %s name1 name2 ...", argv[0]);

    char **pptr;
    const char *name;
    struct hostent *entry;
    char buf[INET6_ADDRSTRLEN + 10];
    while (--argc > 0) {
        name = argv[argc];
        entry = gethostbyname(name);
        if (entry == NULL) {
            error_msg("fail to gethostbyname name:%s error:%s", name, hstrerror(h_errno));
            continue;
        }

        error_msg("entry info for name:%s", name);
        error_msg("official name:%s", entry->h_name);

        for (pptr = entry->h_aliases; *pptr; pptr++)
            error_msg("alias:%s", *pptr);

        for (pptr = entry->h_addr_list; *pptr; pptr++) {
            switch (entry->h_addrtype) {
                case AF_INET:
                    error_msg("ipv4 address:%s", inet_ntop(entry->h_addrtype, *pptr, buf, sizeof(buf)));
                    break;
                case AF_INET6:
                    error_msg("ipv6 address:%s", inet_ntop(entry->h_addrtype, *pptr, buf, sizeof(buf)));
                    break;
                default:
                    error_msg("unknown address type:%d", entry->h_addrtype);
                    break;
            }
        }
    }
    
    return 0;
}

